<?php
/**
 * 
 * Dialog
 *
 * @package      	YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version        	$Id: DialogController.class.php v2.5 2014-06-10 06:59:03  $

 */
namespace Admin\Controller;
class DialogController extends CommonController{
    public function _initialize() {
        parent::_initialize();
    }
    
    public function selectimages() {
        layout(false);
        $activepath = I("get.activepath", '', "urldecode");
        $f = I("get.f", 'form1.picname', 'trim');
        $v = I("get.v", 'picview', 'trim');
        $comeback = I("get.comeback", "", "trim");
        $activepath = preg_replace("#image\/#i", "", $activepath);
        //echo $activepath;die;
        $inpath = YDCMS_ROOT . 'Public/uploads/image' . $activepath;
        $activeurl = '../Public/uploads/image'.$activepath;
        //echo $inpath;die;
        //echo $activeurl;
        //var_dump($inpath);die;
        if(!is_dir($inpath)){
            $this->assign("closeWin", true);
            $this->error("非法操作！", U("index/index"));
        }
        $dh = dir($inpath);
        $ty1="";
        $ty2="";
        $line = "";
        while($file = $dh->read()) {

            //-----计算文件大小和创建时间
            if($file!="." && $file!=".." && !is_dir("$inpath/$file")){
                $filesize = filesize("$inpath/$file");
                $filesize = $filesize / 1024;
                if($filesize != "")
                if($filesize < 0.1){
                    @list($ty1, $ty2) = split("\.", $filesize);
                    $filesize = $ty1.".".substr($ty2, 0, 2);
                }
                else{
                    @list($ty1, $ty2) = split("\.", $filesize);
                    $filesize = $ty1.".".substr($ty2, 0, 1);
                }
                $filetime = filemtime("$inpath/$file");
                $filetime = date("Y-m-d H:i:s", $filetime);
            }

            if($file == ".") continue;
            else if($file == "..")
            {
                if($activepath == "") continue;
                $tmp = preg_replace("#[\/][^\/]*$#i", "", $activepath);
                $url = U("selectimages", array('f' => $f, 'activepath' => urlencode($tmp).$addparm));
                $line .= "\n<tr>
           <td colspan='2'>
           <a href='{$url}'><img src=".__ROOT__."/Public/admin/images/img/dir2.gif border=0 width=16 height=16 align=absmiddle>上级目录</a></td>
           <td colspan='2'> 当前目录:$activepath</td>
           </tr>
           ";
                //echo $line;
            }
            else if(is_dir("$inpath/$file"))
            {
                if(preg_match("#^_(.*)$#i", $file)) continue; #屏蔽FrontPage扩展目录和linux隐蔽目录
                if(preg_match("#^\.(.*)$#i", $file)) continue;
                $url = U("selectimages", array('f' => $f, 'activepath' => urlencode("$activepath/$file").$addparm));
                $line .= "\n<tr>
           <td colspan='2'>
           <a href='{$url}'><img src=".__ROOT__."/Public/admin/images/img/dir.gif border=0 width=16 height=16 align=absmiddle>$file</a></td>
           <td>　</td>
           <td>　</td>
           </tr>";
                //echo "$line";
            }
            else if(preg_match("#\.(gif|png)#i", $file))
            {
                $reurl = "$activeurl/$file";
                $reurl = preg_replace("#^\.\.#", "", $reurl);
                if($cfg_remote_site=='Y' && $remoteuploads == 1)
                 {
                   $reurl  = $remoteupUrl.$reurl;
                }else{
                    $reurl = $reurl;
                }

                if($file==$comeback) $lstyle = " style='color:red' ";
                else  $lstyle = "";

                $line .= "\n<tr>
           <td align='center'>
           <a href=\"#\" onClick=\"ChangeImage('$reurl');\"><img src='".__ROOT__."/Public/admin/images/img/picviewnone.gif' width='16' height='16' border='0' align=absmiddle></a>
           </td>
           <td>
           <a href=# onclick=\"ReturnImg('$reurl');\" $lstyle><img src=".__ROOT__."/Public/admin/images/img/gif.gif border=0 width=16 height=16 align=absmiddle>$file</a></td>
           <td>$filesize KB</td>
           <td align='center'>$filetime</td>
           </tr>";
                //echo "$line";
            }
            else if(preg_match("#\.(jpg)#i", $file))
            {
                $reurl = "$activeurl/$file";
                $reurl = preg_replace("#^\.\.#", "", $reurl);
                if($cfg_remote_site=='Y' && $remoteuploads == 1)
                 {
                   $reurl  = $remoteupUrl.$reurl;
                }else{
                    $reurl = $reurl;
                }

                if($file==$comeback) $lstyle = " style='color:red' ";
                else  $lstyle = "";

                $line .= "\n<tr>
           <td align='center'>
           <a href=\"#\" onClick=\"ChangeImage('$reurl');\"><img src='".__ROOT__."/Public/admin/images/img/picviewnone.gif' width='16' height='16' border='0' align=absmiddle></a>
           </td>
           <td>
           <a href=# onclick=\"ReturnImg('$reurl');\" $lstyle><img src=".__ROOT__."/Public/admin/images/img/jpg.gif border=0 width=16 height=16 align=absmiddle>$file</a>
           </td>
           <td>$filesize KB</td>
           <td align='center'>$filetime</td>
           </tr>";
                //echo "$line";
            }
        }//End Loop
        $dh->close();
        $this->assign('f', $f);
        $this->assign('activepath', $activepath);
        $this->assign('html', $line);
        $this->display();
    }
    
    public function SelectImagesPost() {
        $activepath = preg_replace('#\/#', '', $_POST['activepath'], 1);
        $imgfile = $_FILES['imgfile']['tmp_name'];
        //var_dump($_FILES);die;
        $f = $_POST['f'];
        $v = $_POST['v'];
        //var_dump($imgfile);die;
        if(empty($activepath))
        {
            $activepath ='';
            $activepath = str_replace('.', '', $activepath);
            $activepath = preg_replace('#\/#', '', $activepath, 1);
            $activepath = preg_replace("#\/{1,}#", '/', $activepath);
            if(strlen($activepath) < strlen("image"))
            {
                $activepath = "image";
            }
        }
        /*if(empty($imgfile))
        {
            $imgfile='';
        }
        if(!is_uploaded_file($imgfile))
        {
            $this->error('你没有选择上传的文件!');
            exit();
        }*/
        
        $upload = new \Think\Upload();	//实例化上传类
        $upload->maxSize = C('UPLOAD_MAX_SIZE');	//图片最大上传大小
        $upload->savePath = $activepath . '/';	//图片保存路径
        $upload->replace = true;	//覆盖同名文件
        $upload->exts = C('UPLOAD_IMAGE_EXTS');	//允许上传文件的后缀名
        $upload->mimes = C('UPLOAD_IMAGE_MIMES');	//允许上传文件的后缀名
        $info = $upload->upload();
        if(!$info){
            $this->error($upload->getError());
        }else{
            $mark_conf = fast_cache("inc_photowatermark_config", '', YDCMS_DATA . "mark/");
            if($mark_conf['photo_markup'] == 1){//添加水印
                   
                $image = new \Think\Image(); 
                $filePath = YDCMS_ROOT .  C('UPLOAD_PATH') . $info['imgfile']['savepath'] . $info['imgfile']['savename'];
                
                $pos = $mark_conf['photo_waterpos'] == 0 ? rand(1, 9) : $mark_conf['photo_waterpos'];
                if(($mark_conf['photo_marktype'] == 2 || !file_exist(YDCMS_DATA . "mark/" . $mark_conf['photo_markimg']) && file_exist(YDCMS_DATA . 'mark/simhei.ttf'))){//添加文字水印

                    $image->open($filePath)->text($mark_conf['photo_watertext'], YDCMS_DATA . 'mark/simhei.ttf', $mark_conf['photo_fontsize'], $mark_conf['photo_fontcolor'], $pos)->save($filePath); 

                }else if(file_exist(YDCMS_DATA . "mark/" . $mark_conf['photo_markimg'])){//添加图片水印

                    $image->open($filePath)->water(YDCMS_DATA . "mark/" . $mark_conf['photo_markimg'], $pos, $mark_conf['photo_diaphaneity'])->save($filePath); 

                }
            }
            //$pramas = array("comeback" => basename($info['imgfile']['savename']), 'f' => $f, 'v' => $v, "activepath" => urlencode('/'.$activepath . "/" . date("Ymd", time())), 'd' => time());
            $jumpUrl = U('selectimages');
            $pramas = "comeback=".basename($info['imgfile']['savename'])."&f={$f}&v={$v}&activepath=".urlencode('/'.$activepath . "/" . date("Ymd", time()))."&d=".time();
            //echo $jumpUrl;die;
            $this->success("成功上传一幅图片", $jumpUrl . '?'.$pramas);
        }
    }
}
